package cn.analysys.controller;

import cn.analysys.model.BehaviorDict;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Swagger2构建强大的RESTful API文档
 * @author jiangbang
 * @date 2017/4/14
 */
@RestController
@RequestMapping("/swagger")
public class SwaggerController {


    static Map<Integer, BehaviorDict> behaviorDictMap = Collections.synchronizedMap(new HashMap<Integer, BehaviorDict>());

    @RequestMapping(value={""}, method= RequestMethod.GET)
    @ApiOperation(value="获取行为指标列表", notes="")
    public List<BehaviorDict> getUserList() {
        List<BehaviorDict> r = new ArrayList<BehaviorDict>(behaviorDictMap.values());
        return r;
    }

    @RequestMapping(value="", method=RequestMethod.POST)
    @ApiOperation(value="创建行为指标", notes="根据BehaviorDict对象创建行为指标")
    @ApiImplicitParam(name = "behaviorDict", value = "行为指标实体", required = true, dataType = "BehaviorDict")
    public String postUser(@RequestBody BehaviorDict behaviorDict) {
        behaviorDictMap.put(behaviorDict.getId(), behaviorDict);
        return "success";
    }

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    @ApiOperation(value="获取行为指标详细信息", notes="根据url的id来获取行为指标详细信息")
    @ApiImplicitParam(name = "id", value = "行为指标ID", required = true, dataType = "Integer")
    public BehaviorDict getUser(@PathVariable Integer id) {
        return behaviorDictMap.get(id);
    }


    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    @ApiOperation(value="更新行为指标详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "行为指标ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "behaviorDict", value = "行为指标实体", required = true, dataType = "BehaviorDict")
    })
    public String putUser(@PathVariable Integer id, @RequestBody BehaviorDict behaviorDict) {
        BehaviorDict u = behaviorDictMap.get(id);
        u.setDictCode(behaviorDict.getDictCode());
        behaviorDictMap.put(id, u);
        return "success";
    }


    @ApiOperation(value="删除指标详细", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "指标详细ID", required = true, dataType = "Integer")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id) {
        behaviorDictMap.remove(id);
        return "success";
    }

}
